﻿$(function() {
//Declare variable
var 	tips = $( ".validateTips" ),
		button_name="",
		url="a_customer_history.php", 
		ID="",
		rowSelect="";
		
		ID=$("#custid").val();
		 
		//Load First page
		LoadData(1,true,ID);
	
		//Show Loading bar			
		function showLoader(){
			$( "#resultData tbody:first" ).prepend( "<tr>"
						+"<td colspan=9 class='search-background'>"
						+"<label><img src='./images/ajax-loader.gif'/></label>"
						+"</td>"
						+"</tr>" ); 
			 $('.search-background').fadeIn(900,0);
		}
		//Hide Loading bar			
		function hideLoader(){
			$('.search-background').fadeOut(300);
			$( "#resultData tbody tr" ).remove();
		};
		
		//b=false , don't gennerate paging
		function LoadData(page,b,id){
			$( "#resultData tbody tr" ).remove();
			//Show loading
			showLoader();
			//set criteria
			 //Post 			
			 $.post(url,{'m':'info','id':id} , function(data){
				//alert(data);
				var ar = eval('('+data+')');
				$("#iCode").attr('innerHTML',(ar['Code']));
				$("#iName").attr('innerHTML',(ar['FirstName'] + ' ' + ar['LastName']));
				$("#iBirthday").attr('innerHTML',(ar['BirthDay']));
				$("#iAddress").attr('innerHTML',(ar['Address']));
				$("#iLastAccess").attr('innerHTML',(ar['LastedAccess']));	
				$("#iEmail").attr('innerHTML',(ar['Email']));							
				$("#iTele").attr('innerHTML',(ar['Telephone']));				
			});	
			
			$.post(url,{'m':'history','id':id},function(data){
			 //alert(data);
				var obj=eval("(" + data + ")");
				hideLoader();
				$( "#resultData tbody tr" ).remove();
				$( "#rowCount" ).attr('innerHTML',obj['rows']);
				$( "#resultData tbody" ).append(obj['data']);
			
				//set page
				if (b){
 
					if (parseInt(obj['totalpage'])>1){
						$("#paging_btn").show();
						setPaging(parseInt(obj['totalpage']));
					}else{
						$("#paging_btn").hide();
					}					
					 
				}
				//$("#"+page).css({'background-color' : '#006699'});
			});
		}
 
	$("#btnBack").live('click',function(){
		 
		if ($("#hisFrom").val() != 'm'){
			$("#Act").attr("action", "pos.php");
		}else{
			$("#Act").attr("action", "customer.php");
		}
		$("#Act").submit();
		
		
	});
	
	function setPaging(iTotal){
				 $("#paging_btn").paginate({
				count 		: iTotal,
				start 		: 1,
				display     : 10,
				border					: false,
				text_color  			: '#888',
				background_color    	: '#EEE',	
				text_hover_color  		: 'blue',
				background_hover_color	: '#CFCFCF',
				mouse					: 'press',
				onChange     			: function(page){
											LoadData(page,false);
										  }
			});
		}
});
	